﻿<script lang="ts" name="appPlayerTeamStatistics" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { useAppPlayerTeamStatisticsApi } from '/@/api/team-statistics/appPlayerTeamStatistics';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const appPlayerTeamStatisticsApi = useAppPlayerTeamStatisticsApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
  userId: [{required: true, message: '请选择球员Id！', trigger: 'blur',},],
  teamId: [{required: true, message: '请选择球队Id！', trigger: 'blur',},],
});

// 页面加载时
onMounted(async () => {
  const data = await appPlayerTeamStatisticsApi.getDropdownData(false).then(res => res.data.result) ?? {};
  state.dropdownData.userId = data.userId ?? [];
  state.dropdownData.teamId = data.teamId ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await appPlayerTeamStatisticsApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await appPlayerTeamStatisticsApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="appPlayerTeamStatistics-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="球员Id" prop="userId">
							<el-select clearable filterable v-model="state.ruleForm.userId" placeholder="请选择球员Id">
								<el-option v-for="(item,index) in state.dropdownData.userId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="球队Id" prop="teamId">
							<el-select clearable filterable v-model="state.ruleForm.teamId" placeholder="请选择球队Id">
								<el-option v-for="(item,index) in state.dropdownData.teamId" :key="index" :value="item.value" :label="item.label" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="年份" prop="year">
							<el-date-picker v-model="state.ruleForm.year" type="year" placeholder="年份" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="进球数" prop="goals">
							<el-input-number v-model="state.ruleForm.goals" placeholder="请输入进球数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="助攻数" prop="assists">
							<el-input-number v-model="state.ruleForm.assists" placeholder="请输入助攻数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="出勤次数" prop="attendances">
							<el-input-number v-model="state.ruleForm.attendances" placeholder="请输入出勤次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="积分" prop="points">
							<el-input-number v-model="state.ruleForm.points" placeholder="请输入积分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="黄牌数" prop="yellowCards">
							<el-input-number v-model="state.ruleForm.yellowCards" placeholder="请输入黄牌数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="红牌数" prop="redCards">
							<el-input-number v-model="state.ruleForm.redCards" placeholder="请输入红牌数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="上场时间（分钟）" prop="minutesPlayed">
							<el-input-number v-model="state.ruleForm.minutesPlayed" placeholder="请输入上场时间（分钟）" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="射门次数" prop="shots">
							<el-input-number v-model="state.ruleForm.shots" placeholder="请输入射门次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="射正次数" prop="shotsOnTarget">
							<el-input-number v-model="state.ruleForm.shotsOnTarget" placeholder="请输入射正次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="传球次数" prop="passes">
							<el-input-number v-model="state.ruleForm.passes" placeholder="请输入传球次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="传球成功率" prop="passAccuracy">
							<el-input-number v-model="state.ruleForm.passAccuracy" placeholder="请输入传球成功率" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="关键传球次数" prop="keyPasses">
							<el-input-number v-model="state.ruleForm.keyPasses" placeholder="请输入关键传球次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="抢断次数" prop="tackles">
							<el-input-number v-model="state.ruleForm.tackles" placeholder="请输入抢断次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="拦截次数" prop="interceptions">
							<el-input-number v-model="state.ruleForm.interceptions" placeholder="请输入拦截次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="解围次数" prop="clearances">
							<el-input-number v-model="state.ruleForm.clearances" placeholder="请输入解围次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="扑救次数" prop="saves">
							<el-input-number v-model="state.ruleForm.saves" placeholder="请输入扑救次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="扑救成功率" prop="saveAccuracy">
							<el-input-number v-model="state.ruleForm.saveAccuracy" placeholder="请输入扑救成功率" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="犯规次数" prop="fouls">
							<el-input-number v-model="state.ruleForm.fouls" placeholder="请输入犯规次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="被犯规次数" prop="foulsDrawn">
							<el-input-number v-model="state.ruleForm.foulsDrawn" placeholder="请输入被犯规次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="越位次数" prop="offsides">
							<el-input-number v-model="state.ruleForm.offsides" placeholder="请输入越位次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="头球争顶成功次数" prop="aerialDuelsWon">
							<el-input-number v-model="state.ruleForm.aerialDuelsWon" placeholder="请输入头球争顶成功次数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="评分" prop="rating">
							<el-input-number v-model="state.ruleForm.rating" placeholder="请输入评分" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" maxlength="256" show-word-limit clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>